﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using Model.ViewModel;

namespace Model
{
    #region 使用步骤
    //1、工具-库程序包管理器-程序包管理控制平台，输入“Install-Package AutoMapper”命令，就可以把AutoMapper添加到项目中
    //2、添加AutoMapperHelper
    //3、AutoMapperHelper.InitMap();
    //4、设置映射
    //5、调用 
    #endregion

    /// <summary>
    /// 映射工具类
    /// </summary>
    public class AutoMapperHelper
    {
        public static void InitMap()
        {
            #region 参考
            //#region 变换映射
            //Mapper.CreateMap<UserModel, UserViewModel>()
            //        .ForMember(dest => dest.Birthyear, opt => opt.MapFrom(src => src.Birthday.Year.ToString()))
            //        .ForMember(des => des.NameEx, opt => opt.MapFrom(src => src.GetNameEx()))
            //        .ForMember(des => des.AAA, opt => opt.Ignore()); 
            //#endregion

            //#region 内部类映射
            //Mapper.CreateMap<OutModel, OutViewModel>()
            //        .ForMember(des => des.InViewModel, opt => opt.MapFrom(src => src.InModel));
            //Mapper.CreateMap<InModel, InViewModel>(); 
            //#endregion

            //#region 父类子类映射
            //Mapper.CreateMap<ParentModel, ParentViewModel>().Include<ChildModel, ChildViewModel>();
            //Mapper.CreateMap<ChildModel, ChildViewModel>(); 
            //#endregion

            //#region 调用
            //OutViewModel outViewModel = outModel.Map<OutViewModel>(); 
            //#endregion 
            #endregion

            Mapper.CreateMap<Permission, PermissionViewModel>();
            //各种条件映射
            //Mapper.CreateMap<PermissionViewModel, Permission>()
            //    .ForMember(des=>des.RolePermission,opt=>opt.Ignore())
            //    .ForMember(des=>des.pAddTime,opt=>opt.Ignore())
            //    .ForMember(des=>des.pIco,opt=>opt.Ignore())
            //    .ForMember(des=>des.,opt=>opt.Ignore())
            //    .ForMember(des=>des.RolePermission,opt=>opt.Ignore())
               

            Mapper.AssertConfigurationIsValid();
        }
    }

    /// <summary>
    /// 扩展Map方法
    /// </summary>
    public static class AutoMapperExterntion
    {
        public static T Map<T>(this object src)
        {
            T destination = Mapper.Map<T>(src);
            return destination;
        }
    }
}
